package admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"goapp/database"
	"net/http"
)

type User struct {
	Id	         uint32	        `json:"id"`
	Name         string 	    `json:"name"`
	Createtime   string         `json:"create_time"`
}
//列表页
func (user *User) Index (c *gin.Context){
	c.HTML(http.StatusOK, "user_index.html", gin.H{
		"title": "用户列表",
	})
}

func (user *User) Lists (c *gin.Context){

	db := database.GetDb()
	rows, err := db.Query("SELECT id,name,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as create_time FROM users order by createtime desc")
	if err != nil {
		fmt.Println(err)
	}
	defer rows.Close()
	persons := make([]User, 0)

	fmt.Println(rows)

	//数据添加到数据集中
	for rows.Next() {
		var person User
		rows.Scan(&person.Id, &person.Name, &person.Createtime)
		persons = append(persons, person)
	}
	if err = rows.Err(); err != nil {
		fmt.Println()
	}
	c.JSON(200,gin.H{
		"data":persons,
	})
}

